package com.gupao.javavip.gof.prototype;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ArticleService {

    public void batchSave() {
        List<Article> articles = new ArrayList<>();

        for (int i = 1; i <= 100000; i++) {
            Article article = buildtArticle(i);

            articles.add(article);
        }

        save(articles);
    }

    private Article buildtArticle(int i) {
        Date now = new Date();

        User user = new User();
        user.setId("0");
        user.setName("张三");
        user.setAge("20");
        user.setSex("男");
        user.setAddress("成都");
        user.setSchool("北大");
        user.setAvatar("avatar.jpg");

        Article article = new Article();
        article.setId(String.valueOf(i));
        article.setTitle("文章" + i);
        article.setContent("文章内容文章内容文章内容文章内容文章内容文章内容文章内容");
        article.setTime(now.toString());
        article.setReadTime("0");
        article.setType("Java");
        article.setUser(user);


        List<Comment> comments = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            User commentUser = new User();
            commentUser.setId(String.valueOf(j + 1));
            commentUser.setName("张三" + j);
            commentUser.setAge("20");
            commentUser.setSex("男");
            commentUser.setAddress("成都");
            commentUser.setSchool("北大");
            commentUser.setAvatar("avatar.jpg");

            Comment comment = new Comment();
            comment.setCommentUser(commentUser);
            comment.setContent("赞");
            comment.setId(String.valueOf(j));
            comment.setParentId("0");
            comment.setTime(now.toString());

            comments.add(comment);
        }

        article.setComments(comments);
        return article;
    }

    public void batchSavePrototype() {
        List<Article> articles = new ArrayList<>();

        Article article = buildtArticle(1);

        for (int i = 0; i < 100000; i++) {
            article = article.clone();
            article.setId(String.valueOf(i + 1));

            articles.add(article);
        }

        save(articles);
    }

    private void save(List<Article> articles) {
        System.out.printf("保存" + articles.size() + "条文章数据");
    }
}
